package com.dosen.api.third.party.cmp.client.impl;

import cn.hutool.core.net.URLEncoder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.dosen.api.common.asserts.AssertNP;
import com.dosen.api.common.util.information.JSONUtil;
import com.dosen.api.common.util.network.DosenResponse;
import com.dosen.api.common.util.network.HttpUtils;
import com.dosen.api.common.util.network.MeResponse;
import com.dosen.api.third.party.cmp.client.CmpClient;
import com.dosen.api.third.party.cmp.config.CmpApiProperties;
import com.dosen.api.third.party.cmp.model.*;
import com.dosen.api.third.party.cmp.model.hostinfo.AssignedLicense;
import com.dosen.api.third.party.cmp.model.hostinfo.CmpHost;
import com.dosen.api.third.party.cmp.model.hostinfo.CmpHostParam;
import com.dosen.api.third.party.cmp.model.hostinfo.HostConfig;
import com.dosen.api.third.party.cmp.model.login.LoginData;
import com.dosen.api.third.party.cmp.model.login.LoginKey;
import com.dosen.api.third.party.cmp.model.overview.AllocationRate;
import com.dosen.api.third.party.cmp.model.overview.ReportsParam;
import com.dosen.api.third.party.cmp.model.overview.StatsParam;
import com.dosen.api.third.party.cmp.model.overview.UtilizationRate;
import com.dosen.api.third.party.cmp.model.vdc.CmpVDataCenter;
import com.dosen.api.third.party.cmp.model.vdc.VDataCenterParam;
import com.dosen.api.third.party.cmp.model.vhostinfo.CmpVHost;
import com.dosen.api.third.party.cmp.model.vhostinfo.CmpVHostParam;
import com.dosen.api.third.party.cmp.model.vhostinfo.CmpVmCountParam;
import com.dosen.api.third.party.cmp.model.vhostinfo.QueryVmInfoByNameParam;
import com.dosen.api.third.party.cmp.utils.CmpTokenManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.*;


/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2023/1/12 12:16
 * @description:
 */
public class CmpClientImpl implements CmpClient {
    Logger log = LoggerFactory.getLogger(CmpClientImpl.class);

    MeResponse response;
    HttpUtils httpUtils;
    CmpApiProperties cmpApiProperties;

    public CmpClientImpl() {
    }

    public CmpClientImpl(MeResponse response, HttpUtils httpUtils, CmpApiProperties cmpApiProperties) {
        this.response = response;
        this.httpUtils = httpUtils;
        this.cmpApiProperties = cmpApiProperties;
    }


    /**
     * 登录API与其他API的端口不一样
     * http://10.10.33.48:60005/api/soc/v1/login
     * form表单参数 sip_test/Cmp@v587
     * account: sip_test
     * password: hmCXfEzrYqgg0Nt0c4H5mw==
     *
     * @return
     */
    public LoginData getLoginData() {
        String preUrl = cmpApiProperties.getPreUrlOfLogin();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_ACCOUNT_LOGIN, cmpApiProperties.getRealUsername());
        urlParamPair.put(CommonConst.PARAM_PASSWORD_LOGIN, cmpApiProperties.getRealPassword());
        //
        LoginKey key = new LoginKey();
        key.setAccount(cmpApiProperties.getRealUsername()).setPassword(cmpApiProperties.getRealPassword());
        //
        Map<String, String> formParam = new HashMap<>();
        formParam.put(CommonConst.PARAM_ACCOUNT_LOGIN, cmpApiProperties.getRealUsername());
        formParam.put(CommonConst.PARAM_PASSWORD_LOGIN, cmpApiProperties.getRealPassword()/*HashUtil.md5Hash(cmpApiProperties.getRealPassword())*/);
        DosenResponse dosenResponse = response.getContent(CommonConst.PARAM_HTTP_POST, preUrl + CommonConst.API_LOGIN_URL,
                null, urlParamPair, key, formParam, null);
        if (!AssertNP.legalParams(dosenResponse)) {
            log.info("请求结果为空");
            return new LoginData();
        }
        int httpStatusCode = dosenResponse.getHttpStatusCode();
        if (httpStatusCode - 200 != 0) {
            log.info(dosenResponse.toString());
            return new LoginData();
        }
        CmpResult cmpResult = JSONUtil.getT(new String(dosenResponse.getContent()), new TypeReference<CmpResult>() {
        });
        Object data = cmpResult.getData();
        if (Boolean.TRUE.equals(!cmpResult.success) || !AssertNP.legalParams(data)) {
            log.info("LoginData为空");
            return new LoginData();
        }
        LoginData loginData = JSONUtil.getT(JSON.toJSONString(data), new TypeReference<LoginData>() {
        });
        CmpTokenManager.CMP_TOKEN_MAP.put(CommonConst.PARAM_CMP_TOKEN, loginData.getToken());
        return loginData;
    }

    /**
     * 获取token
     *
     * @return
     */
    public String token() {
        String token = CmpTokenManager.CMP_TOKEN_MAP.get(CommonConst.PARAM_CMP_TOKEN);
        while (!AssertNP.legalParams(token) || !tokenCheck(token)) {
            getLoginData();
            token = CmpTokenManager.CMP_TOKEN_MAP.get(CommonConst.PARAM_CMP_TOKEN);
        }
        return CmpTokenManager.CMP_TOKEN_MAP.get(CommonConst.PARAM_CMP_TOKEN);
    }

    /**
     * http://10.10.33.48:60006/api/soc/v1/token?t=1673526249156&token=eyJ0eXAiOiJKV1Q...6YPc7bNfM
     *
     * @param token
     * @return
     */
    public boolean tokenCheck(String token) {
        String preUrl = cmpApiProperties.getPreUrl();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_T, System.currentTimeMillis());
        urlParamPair.put(CommonConst.PARAM_CMP_TOKEN, token);
        Map<String, String> headers = new HashMap<>();
        headers.put(CommonConst.PARAM_CMP_TOKEN, token);
        //q
        DosenResponse content = response.getContent(CommonConst.PARAM_HTTP_GET, preUrl + CommonConst.API_TOKEN_CHECK_URL, null,
                urlParamPair, null, null, headers);
        if (null == content) {
            log.info(CommonConst.REQUEST_RESULT_EMPTY);
        } else {
            int httpStatusCode = content.getHttpStatusCode();
            if (httpStatusCode - 200 != 0) {
                log.info(String.format("%s / %s", CommonConst.HTTP_STATUS_CODE_NOT_200, content.toString()));
            } else {
                CmpResult cmpResult = JSONUtil.getT(new String(content.getContent()), new TypeReference<CmpResult>() {
                });
                if (CommonConst.TOKEN_CHECK_SUCCESS.equals(cmpResult.getMessage())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * ===============================================综合云管平台信息返回======================
     */
    public CmpInfo cmpInfo() {
        String protocol = cmpApiProperties.getProtocol();
        String realHost = cmpApiProperties.getRealHost();
        String realLoginPort = cmpApiProperties.getRealLoginPort();
        String realPort = cmpApiProperties.getRealPort();
        CmpInfo cmpInfo = new CmpInfo();
        cmpInfo.setProtocol(protocol).setHost(realHost).setLoginPort(realLoginPort).setPort(realPort);
        return cmpInfo;
    }

    /**
     * ===============================================云平台===================================
     * http://10.10.33.48:60006/api/ims/v1/vendors?t=1673532120605&page=1&rows=12&params=[{"param":{"vendorCategory":"COMPUTE"},"sign":"EQ"}]
     *
     * @param page   1
     * @param rows   10
     * @param params [{"param":{"vendorCategory":"COMPUTE"},"sign":"EQ"}]
     */
    public List<CloudPlatform> cloudPlatformList(String page, String rows, List<V1VendorsParams> params) {
        String preUrl = cmpApiProperties.getPreUrl();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_T, System.currentTimeMillis());
        urlParamPair.put("page", page);
        urlParamPair.put("rows", rows);
        urlParamPair.put("params", URLEncoder.DEFAULT.encode(JSON.toJSONString(params), StandardCharsets.UTF_8));
        Map<String, String> headers = new HashMap<>();
        headers.put(CommonConst.PARAM_CMP_TOKEN, token());
        //
        DosenResponse content = response.getContent(CommonConst.PARAM_HTTP_GET, preUrl + CommonConst.API_CLOUD_PLATFORM_URL, null,
                urlParamPair, null, null, headers);
        if (null == content) {
            log.info(CommonConst.REQUEST_RESULT_EMPTY);
            return Collections.emptyList();
        }
        int httpStatusCode = content.getHttpStatusCode();
        if (httpStatusCode - 200 != 0) {
            log.info(String.format("%s / %s", CommonConst.HTTP_STATUS_CODE_NOT_200, content.toString()));
            return Collections.emptyList();
        }
        CmpResult cmpResult = JSONUtil.getT(new String(content.getContent()), new TypeReference<CmpResult>() {
        });
        Object data = cmpResult.getData();
        if (Boolean.TRUE.equals(!cmpResult.success) || !AssertNP.legalParams(data)) {
            log.info(String.format("%s / %s", CommonConst.INNER_DATA_EMPTY, cmpResult.toString()));
            return Collections.emptyList();
        }
        Map<String, Object> map = JSONUtil.getT(JSON.toJSONString(data), new TypeReference<Map<String, Object>>() {
        });
        Object res = map.get("rows");
        List<CloudPlatform> result = JSONUtil.getT(JSON.toJSONString(res), new TypeReference<List<CloudPlatform>>() {
        });
        result.forEach(item -> {
            CmpAuthentication cmpAuthentication = item.cmpAuthentication();
            if (null != cmpAuthentication) {
                log.info(String.format("\n" + "%s", cmpAuthentication.toString()));
            }
        });
        return result;
    }

    /**
     * @param page           1
     * @param rows           10
     * @param vendorCategory COMPUTE
     * @return
     */
    public List<CloudPlatform> cloudPlatformList0(String page, String rows, String vendorCategory) {
        List<V1VendorsParams> params = new ArrayList<>();
        V1VendorsParams v1VendorsParams = new V1VendorsParams();
        V1VendorsParams.ParamDTO paramDTO = new V1VendorsParams.ParamDTO();
        paramDTO.setVendorCategory(vendorCategory);
        v1VendorsParams.setParam(paramDTO);
        v1VendorsParams.setSign("EQ");
        params.add(v1VendorsParams);
        log.info(String.format("\n" + "%s", JSON.toJSONString(params)));
        return cloudPlatformList(page, rows, params);
    }

    /**
     * =============================================宿主机=========================================
     * http://10.10.33.48:60006/api/ims/v1/hosts?t=1673571945844&page=1&rows=10&type=Vmware&params=[{"param":{"vendorId":143},"sign":"EQ"}]
     */
    public List<CmpHost> cmpHostList(String page, String rows, String type, List<CmpHostParam> params) {
        String preUrl = cmpApiProperties.getPreUrl();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_T, System.currentTimeMillis());
        urlParamPair.put("page", page);
        urlParamPair.put("rows", rows);
        urlParamPair.put("type", type);
        urlParamPair.put("params", URLEncoder.DEFAULT.encode(JSON.toJSONString(params), StandardCharsets.UTF_8));
        Map<String, String> headers = new HashMap<>();
        headers.put(CommonConst.PARAM_CMP_TOKEN, token());

        DosenResponse content = response.getContent(CommonConst.PARAM_HTTP_GET, preUrl + CommonConst.API_CMP_HOST_URL, null, urlParamPair,
                null, null, headers);
        if (null == content) {
            log.info(CommonConst.REQUEST_RESULT_EMPTY);
            return Collections.emptyList();
        }
        int httpStatusCode = content.getHttpStatusCode();
        if (httpStatusCode - 200 != 0) {
            log.info(String.format("%s / %s", CommonConst.HTTP_STATUS_CODE_NOT_200, content.toString()));
            return Collections.emptyList();
        }
        CmpResult cmpResult = JSONUtil.getT(new String(content.getContent()), new TypeReference<CmpResult>() {
        });
        Object data = cmpResult.getData();
        Boolean success = cmpResult.getSuccess();
        if (Boolean.TRUE.equals(!success) || !AssertNP.legalParams(data)) {
            log.info(String.format("%s / %s", CommonConst.INNER_DATA_EMPTY, cmpResult.toString()));
            return Collections.emptyList();
        }
        Map<String, Object> map = JSONUtil.getT(JSON.toJSONString(data), new TypeReference<Map<String, Object>>() {
        });
        Object res = map.get("rows");
        List<CmpHost> cmpHosts = JSONUtil.getT(JSON.toJSONString(res), new TypeReference<List<CmpHost>>() {
        });
        cmpHosts.forEach(item -> {
            HostConfig config = item.getConfig();
            if (AssertNP.legalParams(config)) {
                String assignedLicense = config.getAssignedLicense();
                if (AssertNP.legalParams(assignedLicense)) {
                    List<AssignedLicense> assignedLicenses = JSONUtil.getT(assignedLicense, new TypeReference<List<AssignedLicense>>() {
                    });
                    if (AssertNP.legalParams(assignedLicenses)) {
                        assignedLicenses.forEach(e -> log.info(String.format("%s", e.toString())));
                    }
                }
            }
        });
        return cmpHosts;
    }

    /**
     * @param vendorId 1
     * @param type     Vmware
     * @param page     1
     * @param rows     10
     * @return
     */
    public List<CmpHost> cmpHostList0(Integer vendorId, String type, String page, String rows) {
        List<CmpHostParam> params = new ArrayList<>();
        CmpHostParam cmpHostParam = new CmpHostParam();
        CmpHostParam.ParamDTO paramDTO = new CmpHostParam.ParamDTO();
        paramDTO.setVendorId(vendorId);
        cmpHostParam.setParam(paramDTO);
        cmpHostParam.setSign("EQ");
        params.add(cmpHostParam);
        log.info(String.format("\n" + "%s", JSON.toJSONString(params)));
        return cmpHostList(page, rows, type, params);
    }

    /**
     * =================================================指定云平台下的虚拟机========================================
     * http://10.10.33.48:60006/api/ims/v1/vms?t=1673577268821&page=1&rows=10&params=[{"param":{"vendorId":143,"isTemplate":false},"sign":"EQ"}]
     */
    public Map<String, Object> cmpVHostDataMap(String page, String rows, List<CmpVHostParam> params) {
        String preUrl = cmpApiProperties.getPreUrl();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_T, System.currentTimeMillis());
        urlParamPair.put("page", page);
        urlParamPair.put("rows", rows);
        urlParamPair.put("params", URLEncoder.DEFAULT.encode(JSON.toJSONString(params), StandardCharsets.UTF_8));
        Map<String, String> headers = new HashMap<>();
        headers.put(CommonConst.PARAM_CMP_TOKEN, token());
        DosenResponse content = response.getContent(CommonConst.PARAM_HTTP_GET, preUrl + CommonConst.API_CMP_VHOST_URL, null, urlParamPair,
                null, null, headers);
        if (null == content) {
            log.info(CommonConst.REQUEST_RESULT_EMPTY);
            return Collections.emptyMap();
        }
        int httpStatusCode = content.getHttpStatusCode();
        if (httpStatusCode - 200 != 0) {
            log.info(String.format("%s / %s", CommonConst.HTTP_STATUS_CODE_NOT_200, content.toString()));
            return Collections.emptyMap();
        }
        CmpResult cmpResult = JSONUtil.getT(new String(content.getContent()), new TypeReference<CmpResult>() {
        });
        Boolean success = cmpResult.success;
        Object data = cmpResult.getData();
        if (Boolean.TRUE.equals(!success) || !AssertNP.legalParams(data)) {
            log.info(String.format("%s / %s", CommonConst.INNER_DATA_EMPTY, cmpResult.toString()));
            return Collections.emptyMap();
        }
        return JSONUtil.getT(JSON.toJSONString(data), new TypeReference<Map<String, Object>>() {
        });
    }

    /**
     * @param vendorId 云平台id
     * @param page     page
     * @param rows     rows
     * @return
     */
    public List<CmpVHost> cmpVHostList(Integer vendorId, String page, String rows) {
        List<CmpVHostParam> params = new ArrayList<>();
        CmpVHostParam cmpVHostParam = new CmpVHostParam();
        CmpVHostParam.ParamDTO paramDTO = new CmpVHostParam.ParamDTO();
        paramDTO.setVendorId(vendorId);
        paramDTO.setIsTemplate(false);
        cmpVHostParam.setParam(paramDTO);
        cmpVHostParam.setSign("EQ");
        params.add(cmpVHostParam);
        log.info(String.format("%s", JSON.toJSONString(params)));
        //
        Map<String, Object> map = cmpVHostDataMap(page, rows, params);
        if (!AssertNP.legalParams(map)) {
            return Collections.emptyList();
        }
        Object vHostsObj = map.get("rows");
        return JSONUtil.getT(JSON.toJSONString(vHostsObj), new TypeReference<List<CmpVHost>>() {
        });
    }

    /**
     * 根据虚拟机名称查询虚拟机信息
     * http://10.10.33.48:60006/api/ims/v1/vms?t=1673835695921&page=1&rows=10&
     * params=[{"param":{"name":"nfs"},"sign":"LK"},{"param":{"vendorId":143,"isTemplate":false},"sign":"EQ"}]
     */
    public List<CmpVHost> queryCmpVHostsByNameLike(String page, String rows, List<QueryVmInfoByNameParam> params) {
        String preUrl = cmpApiProperties.getPreUrl();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_T, System.currentTimeMillis());
        urlParamPair.put("page", page);
        urlParamPair.put("rows", rows);
        urlParamPair.put("params", URLEncoder.DEFAULT.encode(JSON.toJSONString(params), StandardCharsets.UTF_8));
        Map<String, String> headers = new HashMap<>();
        headers.put(CommonConst.PARAM_CMP_TOKEN, token());
        DosenResponse content = response.getContent(CommonConst.PARAM_HTTP_GET, preUrl + CommonConst.API_VM_INFO_BY_NAME, null,
                urlParamPair, null, null, headers);
        if (null == content) {
            log.info(CommonConst.REQUEST_RESULT_EMPTY);
            return Collections.emptyList();
        }
        int httpStatusCode = content.getHttpStatusCode();
        if (httpStatusCode - 200 != 0) {
            log.info(String.format("%s / %s", CommonConst.HTTP_STATUS_CODE_NOT_200, content.toString()));
            return Collections.emptyList();
        }
        CmpResult cmpResult = JSONUtil.getT(new String(content.getContent()), new TypeReference<CmpResult>() {
        });
        Boolean success = cmpResult.success;
        Object data = cmpResult.getData();
        if (Boolean.TRUE.equals(!success) || !AssertNP.legalParams(data)) {
            log.info(String.format("%s%s", CommonConst.INNER_DATA_EMPTY, cmpResult.toString()));
            return Collections.emptyList();
        }
        Map<String, Object> dataMap = JSONUtil.getT(JSON.toJSONString(data), new TypeReference<Map<String, Object>>() {
        });
        Object vmListObj = dataMap.get("rows");
        return JSONUtil.getT(JSON.toJSONString(vmListObj), new TypeReference<List<CmpVHost>>() {
        });
    }

    /**
     * 简化接口调用
     *
     * @param vmName   myvm01
     * @param vendorId 100
     * @param page     1
     * @param rows     10
     * @return
     */
    public List<CmpVHost> queryCmpVHostsByNameLike0(String vmName, Integer vendorId, String page, String rows) {
        QueryVmInfoByNameParam param1 = new QueryVmInfoByNameParam();
        QueryVmInfoByNameParam.ParamDTO paramDTO1 = new QueryVmInfoByNameParam.ParamDTO();
        paramDTO1.setName(vmName);
        param1.setParam(paramDTO1);
        param1.setSign("LK");
        QueryVmInfoByNameParam param2 = new QueryVmInfoByNameParam();
        QueryVmInfoByNameParam.ParamDTO paramDTO2 = new QueryVmInfoByNameParam.ParamDTO();
        paramDTO2.setVendorId(vendorId);
        paramDTO2.setIsTemplate(false);
        param2.setParam(paramDTO2);
        param2.setSign("EQ");
        List<QueryVmInfoByNameParam> params = new ArrayList<>();
        params.add(param1);
        params.add(param2);
        return queryCmpVHostsByNameLike(page, rows, params);
    }

    /**
     * allVHost
     *
     * @param vendorId 云平台id
     */
    public List<CmpVHost> allVHost(Integer vendorId) {
        List<CmpVHostParam> params = new ArrayList<>();
        CmpVHostParam cmpVHostParam = new CmpVHostParam();
        CmpVHostParam.ParamDTO paramDTO = new CmpVHostParam.ParamDTO();
        paramDTO.setVendorId(vendorId);
        paramDTO.setIsTemplate(false);
        cmpVHostParam.setParam(paramDTO);
        cmpVHostParam.setSign("EQ");
        params.add(cmpVHostParam);
        log.info(String.format("\n" +
                " %s", JSON.toJSONString(params)));
        //
        Map<String, Object> map = cmpVHostDataMap("1", "1", params);
        if (!AssertNP.legalParams(map)) {
            return Collections.emptyList();
        }
        Map<String, Object> res = cmpVHostDataMap("1", map.get("total") + "", params);
        if (!AssertNP.legalParams(res)) {
            return Collections.emptyList();
        }
        Object vHostsObj = res.get("rows");
        return JSONUtil.getT(JSON.toJSONString(vHostsObj), new TypeReference<List<CmpVHost>>() {
        });
    }

    /**
     * ============================================vmsDataMap 云平台外获取虚拟机接口=========================================
     * http://10.10.33.48:60006/api/ims/v1/vms?t=1673580325327&page=1&rows=10&params=[{"param":{"isTemplate":false},"sign":"EQ"}]
     */
    public Map<String, Object> vmsDataMap(String page, String rows, List<CmpVmCountParam> params) {
        String preUrl = cmpApiProperties.getPreUrl();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_T, System.currentTimeMillis());
        urlParamPair.put("page", page);
        urlParamPair.put("rows", rows);
        urlParamPair.put("params", URLEncoder.DEFAULT.encode(JSON.toJSONString(params), StandardCharsets.UTF_8));
        Map<String, String> headers = new HashMap<>();
        headers.put(CommonConst.PARAM_CMP_TOKEN, token());
        DosenResponse content = response.getContent(CommonConst.PARAM_HTTP_GET, preUrl + CommonConst.API_CMP_VM_COUNT, null,
                urlParamPair, null, null, headers);
        if (null == content) {
            log.info(CommonConst.REQUEST_RESULT_EMPTY);
            return Collections.emptyMap();
        }
        int httpStatusCode = content.getHttpStatusCode();
        if (httpStatusCode - 200 != 0) {
            log.info(String.format("%s / %s", CommonConst.HTTP_STATUS_CODE_NOT_200, content.toString()));
            return Collections.emptyMap();
        }
        CmpResult cmpResult = JSONUtil.getT(new String(content.getContent()), new TypeReference<CmpResult>() {
        });
        Boolean success = cmpResult.success;
        Object data = cmpResult.getData();
        if (!success || !AssertNP.legalParams(data)) {
            log.info(String.format("%s / %s", CommonConst.INNER_DATA_EMPTY, cmpResult.toString()));
            return Collections.emptyMap();
        }
        return JSONUtil.getT(JSON.toJSONString(data), new TypeReference<Map<String, Object>>() {
        });
    }

    /**
     * byName
     * http://10.10.33.48:60006/api/ims/v1/vms?t=1673840024296&page=1&rows=10&params=[{"param":{"name":"nfs"},"sign":"LK"},{"param":{"isTemplate":false},"sign":"EQ"}]
     */
    public List<CmpVHost> queryCmpVmsByName(String name, String page, String rows) {
        List<CmpVmCountParam> params = new ArrayList<>();
        CmpVmCountParam p1 = new CmpVmCountParam();
        CmpVmCountParam.ParamDTO pdo1 = new CmpVmCountParam.ParamDTO();
        pdo1.setName(name);
        p1.setParam(pdo1);
        p1.setSign("LK");
        CmpVmCountParam p2 = new CmpVmCountParam();
        CmpVmCountParam.ParamDTO pdo2 = new CmpVmCountParam.ParamDTO();
        pdo2.setIsTemplate(false);
        p2.setParam(pdo2);
        p2.setSign("EQ");
        params.add(p1);
        params.add(p2);
        Map<String, Object> map = vmsDataMap(page, rows, params);
        if (!AssertNP.legalParams(map)) {
            log.info("vmsDataMap is empty");
            return Collections.emptyList();
        }
        Object objList = map.get("rows");
        return JSONUtil.getT(JSON.toJSONString(objList), new TypeReference<List<CmpVHost>>() {
        });
    }

    /**
     * @param page 1
     * @param rows 10
     * @return
     */
    public List<CmpVHost> cmpVms(String page, String rows) {
        List<CmpVmCountParam> params = new ArrayList<>();
        CmpVmCountParam cmpVmCountParam = new CmpVmCountParam();
        CmpVmCountParam.ParamDTO paramDTO = new CmpVmCountParam.ParamDTO();
        paramDTO.setIsTemplate(false);
        cmpVmCountParam.setParam(paramDTO);
        cmpVmCountParam.setSign("EQ");
        params.add(cmpVmCountParam);
        log.info(String.format("\n" +
                " %s", JSON.toJSONString(params)));
        Map<String, Object> map = vmsDataMap(page, rows, params);
        if (!AssertNP.legalParams(map)) {
            return Collections.emptyList();
        }
        Object vmsObj = map.get("rows");
        return JSONUtil.getT(JSON.toJSONString(vmsObj), new TypeReference<List<CmpVHost>>() {
        });
    }

    /**
     * 虚拟机数量
     *
     * @return
     */
    public Integer vmsCount() {
        List<CmpVmCountParam> params = new ArrayList<>();
        CmpVmCountParam cmpVmCountParam = new CmpVmCountParam();
        CmpVmCountParam.ParamDTO paramDTO = new CmpVmCountParam.ParamDTO();
        paramDTO.setIsTemplate(false);
        cmpVmCountParam.setParam(paramDTO);
        cmpVmCountParam.setSign("EQ");
        params.add(cmpVmCountParam);
        log.info(String.format("\n" +
                " %s", JSON.toJSONString(params)));
        Map<String, Object> map = vmsDataMap("1", "1", params);
        return (Integer) map.get("total");
    }

    /**
     * vms
     *
     * @return
     */
    public List<CmpVHost> cmpAllVm() {
        List<CmpVmCountParam> params = new ArrayList<>();
        CmpVmCountParam cmpVmCountParam = new CmpVmCountParam();
        CmpVmCountParam.ParamDTO paramDTO = new CmpVmCountParam.ParamDTO();
        paramDTO.setIsTemplate(false);
        cmpVmCountParam.setParam(paramDTO);
        cmpVmCountParam.setSign("EQ");
        params.add(cmpVmCountParam);
        log.info(String.format("\n" +
                " %s", JSON.toJSONString(params)));
        Map<String, Object> map = vmsDataMap("1", vmsCount() + "", params);
        Object vmsObj = map.get("rows");
        return JSONUtil.getT(JSON.toJSONString(vmsObj), new TypeReference<List<CmpVHost>>() {
        });
    }

    /**
     * ========================================指定云平台下的数据中心======================================================
     * http://10.10.33.48:60006/api/ims/v1/vdcs?t=1673591693062&page=1&rows=10&params=[{"param":{"vendorId":143},"sign":"EQ"}]
     *
     * @param page   1
     * @param rows   10
     * @param params [{"param":{"vendorId":143},"sign":"EQ"}]
     */
    public Map<String, Object> cmpVDataCenterMap(String page, String rows, List<VDataCenterParam> params) {
        String preUrl = cmpApiProperties.getPreUrl();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_T, System.currentTimeMillis());
        urlParamPair.put("page", page);
        urlParamPair.put("rows", rows);
        urlParamPair.put("params", URLEncoder.DEFAULT.encode(JSON.toJSONString(params), StandardCharsets.UTF_8));
        Map<String, String> headers = new HashMap<>();
        headers.put(CommonConst.PARAM_CMP_TOKEN, token());
        DosenResponse content = response.getContent(CommonConst.PARAM_HTTP_GET, preUrl + CommonConst.API_VDC_URL, null,
                urlParamPair, null, null, headers);
        if (null == content) {
            log.info(CommonConst.REQUEST_RESULT_EMPTY);
            return Collections.emptyMap();
        }
        int httpStatusCode = content.getHttpStatusCode();
        if (httpStatusCode - 200 != 0) {
            log.info(String.format("%s / %s", CommonConst.HTTP_STATUS_CODE_NOT_200, content.toString()));
            return Collections.emptyMap();
        }
        CmpResult cmpResult = JSONUtil.getT(new String(content.getContent()), new TypeReference<CmpResult>() {
        });
        Boolean success = cmpResult.success;
        Object data = cmpResult.getData();
        if (Boolean.TRUE.equals(!success) || !AssertNP.legalParams(data)) {
            log.info(String.format("%s / %s", CommonConst.INNER_DATA_EMPTY, cmpResult.toString()));
            return Collections.emptyMap();
        }
        return JSONUtil.getT(JSON.toJSONString(data), new TypeReference<Map<String, Object>>() {
        });
    }

    /**
     * @param vendorId 100
     * @return
     */
    public Integer vdcNum(Integer vendorId) {
        List<VDataCenterParam> params = new ArrayList<>();
        VDataCenterParam vDataCenterParam = new VDataCenterParam();
        VDataCenterParam.ParamDTO paramDTO = new VDataCenterParam.ParamDTO();
        paramDTO.setVendorId(vendorId);
        vDataCenterParam.setSign("EQ");
        vDataCenterParam.setParam(paramDTO);
        params.add(vDataCenterParam);
        log.info(String.format("\n" + "%s", JSON.toJSONString(params)));
        Map<String, Object> map = cmpVDataCenterMap("1", "1", params);
        return ((Integer) map.get("total"));
    }

    /**
     * @param page     1
     * @param rows     10
     * @param vendorId 100
     * @return
     */
    public List<CmpVDataCenter> cmpVDataCenterList0(String page, String rows, Integer vendorId) {
        List<VDataCenterParam> params = new ArrayList<>();
        VDataCenterParam vDataCenterParam = new VDataCenterParam();
        VDataCenterParam.ParamDTO paramDTO = new VDataCenterParam.ParamDTO();
        paramDTO.setVendorId(vendorId);
        vDataCenterParam.setSign("EQ");
        vDataCenterParam.setParam(paramDTO);
        params.add(vDataCenterParam);
        log.info(String.format("\n" + "%s", JSON.toJSONString(params)));
        //
        Map<String, Object> map = cmpVDataCenterMap(page, rows, params);
        Object vdcListObj = map.get("rows");
        return JSONUtil.getT(JSON.toJSONString(vdcListObj), new TypeReference<List<CmpVDataCenter>>() {
        });
    }

    /**
     * @param vendorId 100
     * @return
     */
    public List<CmpVDataCenter> allCmpVDataCenterList(Integer vendorId) {
        List<VDataCenterParam> params = new ArrayList<>();
        VDataCenterParam vDataCenterParam = new VDataCenterParam();
        VDataCenterParam.ParamDTO paramDTO = new VDataCenterParam.ParamDTO();
        paramDTO.setVendorId(vendorId);
        vDataCenterParam.setSign("EQ");
        vDataCenterParam.setParam(paramDTO);
        params.add(vDataCenterParam);
        log.info(String.format("\n" + "%s", JSON.toJSONString(params)));
        //
        Map<String, Object> map = cmpVDataCenterMap("1", vdcNum(vendorId) + "", params);
        Object vdcListObj = map.get("rows");
        return JSONUtil.getT(JSON.toJSONString(vdcListObj), new TypeReference<List<CmpVDataCenter>>() {
        });
    }

    /**
     * ==================================指定云平台资源概览=====================================
     * http://10.10.33.48:60006/api/ims/v1/portal/platform/stats?t=1673597151347&params={"code":"resUsed","vendorId":143,"vendor":"VMWARE"}
     *
     * @param param {"code":"resUsed","vendorId":143,"vendor":"VMWARE"}
     */
    public List<UtilizationRate> utilizationRateList(StatsParam param) {
        String preUrl = cmpApiProperties.getPreUrl();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_T, System.currentTimeMillis());
        urlParamPair.put("params", URLEncoder.DEFAULT.encode(JSON.toJSONString(param), StandardCharsets.UTF_8));
        Map<String, String> headers = new HashMap<>();
        headers.put(CommonConst.PARAM_CMP_TOKEN, token());
        DosenResponse content = response.getContent(CommonConst.PARAM_HTTP_GET, preUrl + CommonConst.API_UTILIZATION_RATE_URL, null,
                urlParamPair, null, null, headers);
        if (null == content) {
            log.info(CommonConst.REQUEST_RESULT_EMPTY);
            return Collections.emptyList();
        }
        int httpStatusCode = content.getHttpStatusCode();
        if (httpStatusCode - 200 != 0) {
            log.info(String.format("%s / %s", CommonConst.HTTP_STATUS_CODE_NOT_200, content.toString()));
            return Collections.emptyList();
        }
        CmpResult cmpResult = JSONUtil.getT(new String(content.getContent()), new TypeReference<CmpResult>() {
        });
        Boolean success = cmpResult.success;
        Object data = cmpResult.getData();
        if (Boolean.TRUE.equals(!success) || !AssertNP.legalParams(data)) {
            log.info(String.format("%s / %s", CommonConst.INNER_DATA_EMPTY, cmpResult.toString()));
            return Collections.emptyList();
        }
        return JSONUtil.getT(JSON.toJSONString(data), new TypeReference<List<UtilizationRate>>() {
        });
    }

    /**
     * {"code":"resUsed","vendorId":143,"vendor":"VMWARE"}
     *
     * @param vendorId 100
     * @return
     */
    public List<UtilizationRate> utilizationRateList0(Integer vendorId) {
        StatsParam statsParam = new StatsParam();
        statsParam.setCode("resUsed");
        statsParam.setVendorId(vendorId);
        statsParam.setVendor("VMWARE");
        return utilizationRateList(statsParam);
    }

    /**
     * http://10.10.33.48:60006/api/cos/v1/operations/reports?t=1673597151348&params={"vendorId":143,"category":"vendor"}
     */
    public AllocationRate allocationRate(ReportsParam param) {
        String preUrl = cmpApiProperties.getPreUrl();
        Map<String, Object> urlParamPair = new HashMap<>();
        urlParamPair.put(CommonConst.PARAM_T, System.currentTimeMillis());
        urlParamPair.put("params", URLEncoder.DEFAULT.encode(JSON.toJSONString(param), StandardCharsets.UTF_8));
        Map<String, String> headers = new HashMap<>();
        headers.put(CommonConst.PARAM_CMP_TOKEN, token());
        DosenResponse content = response.getContent(CommonConst.PARAM_HTTP_GET, preUrl + CommonConst.API_ALLOCATION_RATE_URL, null,
                urlParamPair, null, null, headers);
        if (null == content) {
            log.info(CommonConst.REQUEST_RESULT_EMPTY);
            return null;
        }
        int httpStatusCode = content.getHttpStatusCode();
        if (httpStatusCode - 200 != 0) {
            log.info(String.format("%s / %s", CommonConst.HTTP_STATUS_CODE_NOT_200, content.toString()));
            return null;
        }
        CmpResult cmpResult = JSONUtil.getT(new String(content.getContent()), new TypeReference<CmpResult>() {
        });
        Boolean success = cmpResult.success;
        Object data = cmpResult.getData();
        if (Boolean.TRUE.equals(!success) || !AssertNP.legalParams(data)) {
            log.info(String.format("%s / %s", CommonConst.INNER_DATA_EMPTY, cmpResult.toString()));
            return null;
        }
        return JSONUtil.getT(JSON.toJSONString(data), new TypeReference<AllocationRate>() {
        });
    }

    /**
     * {"vendorId":143,"category":"vendor"}
     *
     * @param vendorId 100
     * @return
     */
    public AllocationRate allocationRate0(Integer vendorId) {
        ReportsParam reportsParam = new ReportsParam();
        reportsParam.setVendorId(vendorId);
        reportsParam.setCategory("vendor");
        return allocationRate(reportsParam);
    }


}

